<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\HomeUserModel;
use App\Http\Controllers\Home\ShowLinksController;
use App\Http\Controllers\Api\IndexApi;
use App\Http\Requests\BeForPasswordChangePasswordValidata;
use App\Model\HomeUserBeforpass;
use Illuminate\Support\Facades\Redis;

class BeForPasswordChangeController extends Controller
{
    // 显示更改密码页面
    public function ShowChangePasswordPage(Request $request)
    {
        // 站点信息类
        $type = new IndexApi;
        $index = new ShowLinksController;

        // 获取站点信息
        $sate = IndexApi::getSate();
        $linksArray = $index->index();

        // 返回没有用户打开找回密码页面
        return view('Home.goods-beforpassword', ['sate' => $sate, 'linksArray' => $linksArray]);
    }

    // 处理更改密码方法
    public function BeforPasswordCHangePassword(BeForPasswordChangePasswordValidata $request)
    {
        // 得到用户
        $mysqluser = HomeUserModel::select('passname', 'phone', 'id')->where('phone', $request->phone)->get();

        // 判断用户是否存在
        if (!isset($mysqluser[0])) {
            // 不存在返回报错信息
            return redirect()->route('home.beforpass')->with('message', '服务器繁忙，请稍后再试');
        }

        // 准备储存对的次数
        $pass1 = 0;
        $pass2 = 0;

        // 获得该用户用过的密码;
        $beforpassword = $mysqluser[0]->beforpass;

        // 遍历判断用过的密码
        foreach ($beforpassword as $v) {
            // 对比密码是否一致
            $bool = password_verify($request->input('password1'), $v->befor_pass);
            // 判断密码是否一致
            if ($bool) {
                // 判断该用户是否修改成功，成功便记录成功次数
                $pass1++;
            }
        }

        // 遍历判断用过的密码
        foreach ($beforpassword as $v) {
            // 对比密码是否一致
            $bool = password_verify($request->input('password2'), $v->befor_pass);
            // 判断密码是否一致
            if ($bool) {
                // 判断该用户是否修改成功，成功便记录成功次数
                $pass2++;
            }
        }
        // 判断成功次数
        if ($pass1 !== 0 && $pass2 !== 0) {
            // 修改失败返回信息
            return redirect()->route('home.beforpass')->with('message', '条件不足，修改失败!');
        }

        // 记录过去密码
        HomeUserBeforpass::insert([
          'user_id'    => $mysqluser[0]->id,
          'befor_pass' => $mysqluser[0]->passname,
        ]);

        // 更改密码
        HomeUserModel::where('id', $mysqluser[0]->id)
        ->update([
              'passname' => password_hash($request->input('password'), PASSWORD_DEFAULT),
        ]);

        // 准备redis键名
        $userkey = 'home_user_hash_phone:'.$mysqluser[0]->phone;

        // 在redis中查找该用户
        $redisuser = Redis::hGetAll($userkey);

        // 判断redis中是否有该用户
        if ($redisuser) {
            Redis::del($userkey);
            Redis::hMSet($userkey, [
              'id'         => $mysqluser[0]->id,
              'username'   => $mysqluser[0]->username,
              'passname'   => password_hash($request->input('password'), PASSWORD_DEFAULT),
              'sex'        => $mysqluser[0]->sex,
              'pic'        => $mysqluser[0]->pic,
              'email'      => $mysqluser[0]->email,
              'phone'      => $mysqluser[0]->phone,
              'user_status'=> $mysqluser[0]->user_status,
              'member'     => $mysqluser[0]->member,
              'created_at' => $mysqluser[0]->createtime,
            ]);
        }
        // 退出登录
        $request->session()->forget('Homeuser-data');
        return redirect()->route('home.login');
    }
}
